/**
 * Created by hqh on 2016/8/8.
 */
$(document).ready(function(){
    //奖励方式选择
    $('#method').bind('change',function(){
        var value = $(this).val();
        switch(value){
            case '1':
                $('#byPerson').show();
                $('#byGrade').hide();
                break;
            case '2':
                $('#byPerson').hide();
                $('#byGrade').show();
        }
    });

    //添加删除等级奖励
    $('.grade-list').delegate('.decrease','click',function(){
        var total = $('.grade-list .grade-wrapper').length,
            isLast = $(this).parents('.grade-wrapper').nextAll().length;
        console.log(total);
        if(isLast==0){  //最后一个元素
            if(total<=2){
                $(this).parents('.grade-wrapper').prev().find('.increase').show().siblings('.decrease').hide();
                $(this).parents('.grade-wrapper').remove();
            }
            else{
                $(this).parents('.grade-wrapper').prev().find('.increase').show().end().remove();
            }
        }
        else{
            $(this).parents('.grade-wrapper').remove();
        }
    });
    $('.grade-list').delegate('.increase','click',function(){
        var gradeText = $(this).parents('.grade-wrapper').find('.grade-text').attr('text');
        gradeText = Number(gradeText)+1;
        var item =
            '<div class="grade-wrapper">' +
            '<div class="form-group">' +
            '<span class="grade-text" text="'+gradeText+'">' + gradeText +'级</span>' +
            '<label class="form-label-sm">人数</label>' +
            '<input class="form-control form-sm-input person-num" type="text" onkeyup="digit(this, 2)" oninput="digit(this,2)">' +
            '</div><br/>' +
            '<div class="form-group form-group-pad">' +
            '<label class="form-label-sm">奖励</label>' +
            '<input class="form-control form-sm-input award-num" type="text" onkeyup="digit(this, 2)" oninput="digit(this,2)">元' +
            '<div class="coupon-buttons">' +
            '<button class="decrease"><i class="iconfont">&#xe60a;</i></button>' +
            '<button class="increase"><i class="iconfont">&#xe607;</i></button>' +
            '</div>' +
            '<i class="mandatory"></i>' +
            '</div>' +
            '</div>';
        $('.grade-list').append(item);
        $(this).siblings('.decrease').show().end().hide();
    });
});

$(document).ready(function(){
    //选择车队
    $('#selectMCBtn').click(function(){
        var content =
            '<div class="select-list">' +
                '<div class="select-wrapper">' +
                    '<i class="icon select-icon"></i><input type="checkbox" /><label class="oc-name">厦门运营中心</label>' +
                    '<ul class="inner-wrapper">' +
                        '<li><input type="checkbox" /><label>厦门车队</label></li>' +
                        '<li><input type="checkbox" /><label>泉州车队</label></li>' +
                        '<li><input type="checkbox" /><label>漳州车队</label></li>' +
                    '</ul>' +
                '</div>' +
                '<div class="select-wrapper">' +
                    '<i class="icon select-icon"></i><input type="checkbox" /><label class="oc-name">福州运营中心</label>' +
                    '<ul class="inner-wrapper">' +
                        '<li><input type="checkbox" /><label>福州车队</label></li>' +
                        '<li><input type="checkbox" /><label>仓山车队</label></li>' +
                    '</ul>' +
                '</div>' +
            '</div>' +
            '<div class="bot-button">' +
                '<input id="confirm" class="btn btn-lg-blue" type="button" value="确认">' +
                '<input id="cancel" class="btn btn-lg-blue" type="button" value="取消">' +
            '</div>';
        layer.open({
            type:1,
            skin:'layui-layer-rim',
            area: ['500px', '300px'],
            title: '选择车队',
            content: content,
            success: function(layero,index){
                selectMC(layero,index);
            }
        });
    });
});

//选择车队
function selectMC(layero,index){
    //全选反选
    layero.find('label.oc-name').click(function(){
        var $target = $(this).prev();
        if($target.prop('checked')){
            $target.prop('checked',false);
            $(this).parents('.select-wrapper').find('.inner-wrapper input[type="checkbox"]').prop('checked',false);
        }
        else{
            $target.prop('checked',true);
            $(this).parents('.select-wrapper').find('.inner-wrapper input[type="checkbox"]').prop('checked',true);
        }
    });

    layero.find('.inner-wrapper li').click(function(){
        var $target = $(this).find('input[type="checkbox"]');
        if($target.prop('checked')){
            $target.prop('checked',false);
            $(this).parent().siblings('input[type="checkbox"]').prop('checked',false);
        }
        else{
            $target.prop('checked',true);
        }
    });

    //下拉子选项显示或隐藏
    layero.find('.select-wrapper .icon.select-icon').click(function(){
        $(this).toggleClass('hidden');
        if($(this).hasClass('hidden')){
            $(this).siblings('.inner-wrapper').hide();
        }
        else{
            $(this).siblings('.inner-wrapper').show();
        }
    });


    layero.find('#confirm').click(function(){
        var selectedText = [];
        layero.find('.inner-wrapper input[type="checkbox"]:checked').each(function(){
            var value = $(this).next().text();
            selectedText.push(value);
        });
        console.log(selectedText);
        $('#selectMC').val(selectedText.join(';'));
        layer.close(index);
    });

    layero.find('#cancel').click(function(){
        layer.close(index);
    });
}


